package com.guc.coroutines

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock
import android.provider.AlarmClock.ACTION_SET_ALARM
import android.util.Log
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.ViewModelProvider
import com.guc.coroutines.databinding.ActivityMainBinding
import com.guc.coroutines.vm.AppInfoViewModel
import com.guc.gviewbinding.inflate

class MainActivity : AppCompatActivity() {

    private lateinit var cameraPermissionLauncher: ActivityResultLauncher<String>
    private lateinit var captureLauncher: ActivityResultLauncher<Intent>
    private val viewModel: AppInfoViewModel by lazy {
        ViewModelProvider.AndroidViewModelFactory.getInstance(application)
            .create(AppInfoViewModel::class.java)
    }
    private val mainBinding: ActivityMainBinding by inflate()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.appInfo.observe(this) {
            mainBinding.tvShow.text = it.toString()
        }
        mainBinding.btnGetData.setOnClickListener {
            Log.e("AppInfoViewModel", "start")
            viewModel.getAppInfo()
            Log.e("AppInfoViewModel", "end")
        }
        mainBinding.btnAnim.setOnClickListener {
            startActivity(Intent(this, AnimatorActivity::class.java))
        }
        mainBinding.btnDB.setOnClickListener {
            startActivity(Intent(this, DatabaseActivity::class.java))
        }
        mainBinding.btnFragment.setOnClickListener {
            startActivity(Intent(this,FragmentDemoActivity::class.java))
        }
        captureLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                Log.e("MainActivity", it.toString())
                mainBinding.tvShow.text = it.data?.getStringExtra("result")

            }
        scan()
        createAlarm()
    }

    /**
     * 创建闹钟
     */
    @SuppressLint("QueryPermissionsNeeded")
    private fun createAlarm() {
        mainBinding.btnCreateAlarm.setOnClickListener {
            val intent = Intent().apply {
                action = ACTION_SET_ALARM
                putExtra(AlarmClock.EXTRA_MESSAGE, "该起床了")
                putExtra(AlarmClock.EXTRA_HOUR, 7)
                putExtra(AlarmClock.EXTRA_MINUTES, 0)
                putExtra(AlarmClock.EXTRA_SKIP_UI, true)
            }
            if (intent.resolveActivity(packageManager) != null) {
                startActivity(intent)
            }
        }
    }

    private fun scan() {
        cameraPermissionLauncher =
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {
                if (it) {
                    captureLauncher.launch(Intent(this, ScanCaptureActivity::class.java))
                } else {
                    Toast.makeText(this, "没有相机权限", Toast.LENGTH_SHORT).show()
                }
            }
        mainBinding.btnScan.setOnClickListener {
            cameraPermissionLauncher.launch(Manifest.permission.CAMERA)
        }
    }
}